﻿using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EkStudio.com.bean.xml.RootInterface;
using EkStudio.com.bean.xml.BillModule;
using EkStudio.com.bean.xml.RootInterface.Field;
using DevExpress.XtraEditors.Controls;
using EkStudio.Com.UI.Interface;

namespace EkStudio.com.ui.form
{
    public partial class JScriptBindForm : DevExpress.XtraEditors.XtraForm
    {

        private BillFormatModule module = null;

        private ProjectUnitBillInterface UnitPanel = null;

        private string redEdit = ""; 

        public JScriptBindForm(BillFormatModule bean, ProjectUnitBillInterface unit)
        {
            InitializeComponent();
            this.module = bean;
            this.UnitPanel = unit;
            this.InitForm();
        }

        private void InitForm()
        {
            if (module.UiField != null && module.UiField.FieldsProperties != null && module.UiField.FieldsProperties.Count > 0)
            {
                foreach (BillUiGroupItems group in module.UiField.FieldsProperties)
                {
                    BoxSheetList.Items.Add(new ListItem(group.Key, group.Caption, group));
                    if (group.Table)
                    {
                        ListSheetBox.Items.Add(new ListItem(group.Key, group.Caption, group));
                    }
                }
            }
        }

        #region 移动事件

        private void AutoQuerySelect_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = true;
                FormLocation = this.Location;
                mouseOffset = Control.MousePosition;
            }
        }

        private void AutoQuerySelect_MouseMove(object sender, MouseEventArgs e)
        {
            int _x = 0;
            int _y = 0;
            if (isMouseDown)
            {
                Point pt = Control.MousePosition;
                _x = mouseOffset.X - pt.X;
                _y = mouseOffset.Y - pt.Y;

                this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y);
            }
        }

        private void AutoQuerySelect_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }

        private bool isMouseDown = false;
        private Point FormLocation;     //form的location
        private Point mouseOffset;      //鼠标的按下位置

        public string RedEdit
        {
            get
            {
                return redEdit;
            }

            set
            {
                redEdit = value;
            }
        }

        public void SetInitRegEdit(string bind)
        {
            this.RegeditText.Text = bind;
        }

        #endregion


        #region ListItem 类

        private class ListItem
        {
            private string key = "";
            private string caption = "";
            private object tag = null;

            public ListItem(string _key,string _caption, object group)
            {
                this.key = _key;
                this.caption = _caption;
                this.tag = group;
            }

            public string Key
            {
                get
                {
                    return key;
                }

                set
                {
                    key = value;
                }
            }

            public string Caption
            {
                get
                {
                    return caption;
                }

                set
                {
                    caption = value;
                }
            }

            public object Tag
            {
                get
                {
                    return tag;
                }

                set
                {
                    tag = value;
                }
            }

            public override string ToString()
            {
                return this.caption;
            }
        }
        #endregion

        #region 事件注册

        private void BoxSheetList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (BoxSheetList.SelectedIndex >= 0)
            {
                ListItem item = (ListItem)BoxSheetList.SelectedItem;
                BoxFieldList.Items.Clear();
                if(item.Tag is BillUiGroupItems)
                {
                    BillUiGroupItems group = (BillUiGroupItems)item.Tag;
                    if (group.Fields != null && group.Fields.Count > 0)
                    {
                        foreach(BillUiFieldsItem field in group.Fields)
                        {
                            BoxFieldList.Items.Add(new ListItem(field.Key, field.Caption, null));
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 注册界面事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListBoxFormEvent_DoubleClick(object sender, EventArgs e)
        {
            if (ListBoxFormEvent.SelectedIndex >= 0)
            {
                ImageListBoxItem item = (ImageListBoxItem)ListBoxFormEvent.SelectedItem;
                if (RegeditText.Text.Trim().Length > 0)
                {
                    RegeditText.Text = string.Format("{0},{1}", RegeditText.Text, item.Value);
                }
                else
                {
                    RegeditText.Text = (string)item.Value;
                }
            }
        }
        /// <summary>
        /// 注册工具栏事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListToolbarEvent_DoubleClick(object sender, EventArgs e)
        {
            if (ListToolbarEvent.SelectedIndex >= 0)
            {
                ImageListBoxItem item = (ImageListBoxItem)ListToolbarEvent.SelectedItem;
                if (RegeditText.Text.Trim().Length > 0)
                {
                    RegeditText.Text = string.Format("{0},{1}", RegeditText.Text, item.Value);
                }
                else
                {
                    RegeditText.Text = (string)item.Value;
                }
            }
        }
        /// <summary>
        /// 注册子表事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListSheetEvent_DoubleClick(object sender, EventArgs e)
        {
            if(ListSheetEvent.SelectedIndex>=0&& ListSheetBox.SelectedIndex >= 0)
            {
                ListItem sheet = (ListItem)ListSheetBox.SelectedItem;
                ImageListBoxItem item = (ImageListBoxItem)ListSheetEvent.SelectedItem;
                string eventStr = string.Format("ZdzEvent.{0}.{1}", sheet.Key,(string)item.Value);
                if (RegeditText.Text.Trim().Length > 0)
                {
                    RegeditText.Text = string.Format("{0},{1}", RegeditText.Text, eventStr);
                }
                else
                {
                    RegeditText.Text = eventStr;
                }
            }
        }
        /// <summary>
        /// 注册字段事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListFieldEvent_DoubleClick(object sender, EventArgs e)
        {
            if (ListFieldEvent.SelectedIndex >= 0 && BoxSheetList.SelectedIndex >= 0 && BoxFieldList.SelectedIndex >= 0) 
            {
                StringBuilder EventStr = new StringBuilder();
                EventStr.Append("module.");
                ListItem group = (ListItem)BoxSheetList.SelectedItem;
                EventStr.Append(group.Key).Append(".");
                ListItem field = (ListItem)BoxFieldList.SelectedItem;
                EventStr.Append(field.Key).Append(".");
                ImageListBoxItem item = (ImageListBoxItem)ListFieldEvent.SelectedItem;
                EventStr.Append(item.Value);
                if (RegeditText.Text.Trim().Length > 0)
                {
                    RegeditText.Text = string.Format("{0},{1}", RegeditText.Text, EventStr.ToString());
                }
                else
                {
                    RegeditText.Text = EventStr.ToString();
                }
                
            }
        }

        

        private void BtnSubmit_Click(object sender, EventArgs e)
        {
            if (UnitPanel.ReadOnly) return;
            redEdit = this.RegeditText.Text;
        }

        #endregion
    }
}